package com.handpay.zztong.hp.klf;

import java.util.List;
import java.util.Vector;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;

public class VposUtility
{
	private static Dialog dialog;
	/**
	 * Constructs the utility.
	 * 
	 */
	public VposUtility()
	{
		super();
	}
	
	
	/**
	 * 是否在后台运行
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isBackground(Context context) {
		try {
			ActivityManager activityManager = (ActivityManager) context
					.getSystemService(Context.ACTIVITY_SERVICE);
			List<RunningAppProcessInfo> appProcesses = activityManager
					.getRunningAppProcesses();
			
			for (RunningAppProcessInfo appProcess : appProcesses) {
				if (appProcess.processName.equals(context.getPackageName())) {
					return appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
				}
			}
			
		}catch (Exception e) {
			return false;
		}
		
		return false;
	}
	
	
	/**
	 * Identifies the substrings in string that are delimited by one string
	 * specified in an array, then places the substrings into a String array.
	 * 
	 * @param os
	 *            oringinal string
	 * @param separator
	 *            string that delimit the substrings
	 * @return An array of substrings.
	 */
	public static String[] splitString(String os, String separator) {
		Vector<String> vector = new Vector<String>();
		
		int index = os.indexOf(separator);
		int start = 0;
		
		while (index >= 0) {
			String str = os.substring(start, index);
			vector.addElement(str);
			start = index + separator.length();
			index = os.indexOf(separator, start);
		}
		
		if (start < os.length())
			vector.addElement(os.substring(start));
		
		String[] ds = new String[vector.size()];
		vector.copyInto(ds);
		return ds;
	}
	
	public static String getCallNumber(String number) {
		try {
			number = number.replace(" ", "");
			number = number.replace("-", "");
			
			while (!TextUtils.isDigitsOnly(number)){
				number = number.substring(1);
			}
			
			return number;
		} catch (Exception e) {
			return "40076 95516";
		}
	}
	
	/**
	 * String转char[]
	 * 
	 * @param string
	 * @return
	 */
	public static char[] toCharArray(String string) {
		if (string == null) {
			return null;
		}
		return string.toCharArray();
	}
	
	// hex展开
		public static String byte2hex(byte[] b)
		{
			String hs = "";
			String stmp = "";
			for (int n = 0; n < b.length; n++)
			{
				stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
				if (stmp.length() == 1)
					hs = hs + "0" + stmp;
				else
					hs = hs + stmp;
			}
			
			return hs.toUpperCase();
		}
		
		/**
		 * Supplement the byte array with the special byte if its length is not the
		 * multiple of the given number.
		 * 
		 * @param src
		 *            original byte array.
		 * @param fill
		 *            the special byte to be filled.
		 * @param multi
		 *            multiple number.
		 * @return The resulting byte array.
		 */
		public static final byte[] supplementByteArray(byte[] src, byte fill,
				int multi)
		{
			int remainder = src.length % multi;
			byte[] result;
			
			int len = ((multi - remainder) % multi) + src.length;
			
			result = new byte[len];
			System.arraycopy(src, 0, result, 0, src.length);
			
			for (int i = src.length; i < result.length; i++)
			{
				result[i] = fill;
			}
			
			return result;
		}
		
		
		/**
		 * Convert byte to hex string.
		 * 
		 * @param byte0
		 *            byte value.
		 * @return hex string of byte.
		 */
		public static final String hexString(byte byte0)
		{// 字节到十六进制的ASCII码转换
			char ac[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
					'B', 'C', 'D', 'E', 'F' };
			char ac1[] = new char[2];
			ac1[0] = ac[byte0 >>> 4 & 0xf];
			ac1[1] = ac[byte0 & 0xf];
			String s = new String(ac1);
			return s;
		}
		
		/**
		 * Convert byte array to hex string.
		 * 
		 * @param bytes
		 *            - byte array.
		 * @return hex string.
		 */
		public static final String hexString(byte[] bytes)
		{
			StringBuffer buffer = new StringBuffer();
			for (int i = 0; i < bytes.length; i++)
			{
				buffer.append(hexString(bytes[i]));
			}
			
			return buffer.toString();
		}
	
}
